package com.example.microservices.logistics.service.impl;

import com.example.microservices.logistics.model.Shipment;
import com.example.microservices.logistics.repository.ShipmentRepository;
import com.example.microservices.logistics.service.ShipmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class ShipmentServiceImpl implements ShipmentService {

    @Autowired
    private ShipmentRepository shipmentRepository;

    @Override
    public List<Shipment> findAllShipments() {
        return shipmentRepository.findAll();
    }

    @Override
    public Shipment findShipmentById(Long id) {
        return shipmentRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Shipment not found with id: " + id));
    }

    @Override
    public List<Shipment> findShipmentsByOrderId(Long orderId) {
        return shipmentRepository.findByOrderId(orderId)
                .map(Collections::singletonList)
                .orElse(Collections.emptyList());
    }

    @Override
    public Shipment findShipmentByTrackingNumber(String trackingNumber) {
        return shipmentRepository.findByTrackingNumber(trackingNumber)
                .orElseThrow(() -> new RuntimeException("Shipment not found with tracking number: " + trackingNumber));
    }

    @Override
    @Transactional
    public Shipment createShipment(Shipment shipment) {
        // 生成物流单号
        shipment.setShipmentNumber("SH" + UUID.randomUUID().toString().substring(0, 8).toUpperCase());
        // 生成运单号
        shipment.setTrackingNumber("TN" + UUID.randomUUID().toString().substring(0, 12).toUpperCase());
        shipment.setStatus("PENDING");
        shipment.setEstimatedDeliveryTime(LocalDateTime.now().plusDays(3));
        
        shipmentRepository.save(shipment);
        return shipment;
    }

    @Override
    @Transactional
    public void deleteShipment(Long id) {
        Shipment existingShipment = findShipmentById(id);
        shipmentRepository.deleteById(id);
    }

    @Override
    @Transactional
    public Shipment updateShipmentStatus(Long id, String status) {
        Shipment shipment = shipmentRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Shipment not found with id: " + id));
        
        shipment.setStatus(status);
        if ("DELIVERED".equals(status)) {
            shipment.setActualDeliveryTime(LocalDateTime.now());
            shipment.setDeliveryNotes("Package delivered successfully");
        }
        
        return shipmentRepository.save(shipment);
    }

    @Override
    public String trackShipment(String trackingNumber) {
        Shipment shipment = findShipmentByTrackingNumber(trackingNumber);
        return shipment.getStatus();
    }

    @Override
    @Transactional
    public Shipment updateShipment(Long id, Shipment shipment) {
        Shipment existingShipment = findShipmentById(id);
        
        // 更新相关字段
        existingShipment.setStatus(shipment.getStatus());
        existingShipment.setTrackingNumber(shipment.getTrackingNumber());
        existingShipment.setEstimatedDeliveryTime(shipment.getEstimatedDeliveryTime());
        // ... 其他需要更新的字段
        
        return shipmentRepository.save(existingShipment);
    }
} 